package com.jy.wms.service;

import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.pojo.*;
import com.jy.wms.dao.WmsPdaShipmentMapper;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: WmsPdaShipmentService.java
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author Yjs
 * @Date Tue Feb 06 10:50:00 GMT+08:00 2018
 */

@Service
public class WmsPdaShipmentService extends BaseService<WmsPdaShipmentMapper, WmsPdaShipment> {
    /**
     * 分页查询发运任务信息
     * @param entity 实体
     * @return Page<WmsPdaShipment>
     */
    @BaseServiceDescribe(moduleName = "pda发运管理", btnName = "分页查询pda发运任务信息")
    public Page<WmsPdaShipment> baseSelectPage(WmsPdaShipment entity) throws Exception {
        try{
            PageHelper.startPage(entity.getPage(),entity.getRows());
            return (Page<WmsPdaShipment>)dao.baseSelectPage(entity);
        }catch(Exception e){
            throw new ServiceException("查询pda发运任务信息失败", e);
        }
    }

    /**
     * 下发发运任务
     *
     * @param wmsPdaShipmentList
     * @throws Exception
     */
    @BaseServiceDescribe(moduleName = "pda发运管理", btnName = "下发发运任务")
    @Transactional
    public void addPda(List<WmsPdaShipment> wmsPdaShipmentList) throws Exception {
        //回滚pda下发发运任务
        List<String> pdaString = new ArrayList<>();
        //回滚拣货表:发运任务下发状态
        List<String> xfztString = new ArrayList<>();
        //循环发运订单信息
        for (WmsPdaShipment wmsPdaShipment : wmsPdaShipmentList) {
            //查询发运订单信息(状态(0、初始 1、部分分配2、全部分配 3、部分发运4、全部发运5、订单取消6、订单完成))
            WmsShipmentOrder wmsShipmentOrder = dao.searchOrder(wmsPdaShipment.getFyid());
            if ("4".equals(wmsShipmentOrder.getFyzt())) {
                block(pdaString, xfztString);
                throw new ServiceException("发运编号" + wmsShipmentOrder.getFybh() + ",已全部发运不可执行下发任务");
            } else {
                //拣货状态（0、初始 1、部分拣货 2、全部拣货）
                if ("0".equals(wmsShipmentOrder.getJhzt())) {
                    block(pdaString, xfztString);
                    throw new ServiceException("请选择拣货状态为部分拣货或者全部拣货的数据");
                } else {
                    //查询拣货明细信息(发运状态 0:初始;发运任务下发状态 0：初始 )
                    List<WmsPdaShipment> wmsPdaShipmentsList = dao.searchPicking(wmsPdaShipment.getFyid());
                    //循环拣货明细信息
                    for (WmsPdaShipment wmsPdaShipment1 : wmsPdaShipmentsList) {
                        wmsPdaShipment1.setId(UUIDUtils.uuid());
                        //作业人员ID
                        wmsPdaShipment1.setZyryid(wmsPdaShipment.getZyryid());
                        //作业人员名称
                        wmsPdaShipment1.setZyrymc(wmsPdaShipment.getZyrymc());
                        //添加pda发运任务
                        Integer i = dao.addPda(wmsPdaShipment1);
                        if (i > 0) {
                            pdaString.add(wmsPdaShipment1.getId());
                            //更改拣货表:发运任务下发状态 0：初始 1：已下发
                            i = dao.updateXfzt(wmsPdaShipment1.getJhid(), "1");
                            if (i > 0) {
                                xfztString.add(wmsPdaShipment1.getJhid());
                            } else {
                                block(pdaString, xfztString);
                                throw new ServiceException("更改下发发运任务状态失败");
                            }
                        } else {
                            block(pdaString, xfztString);
                            throw new ServiceException("下发发运任务失败");
                        }
                    }
                }
            }
        }
    }

    /**
     * 下发发运任务错误回滚
     *
     * @param pdaString
     * @param xfztString
     */
    public void block(List<String> pdaString, List<String> xfztString) {
        if (pdaString != null && pdaString.size() > 0) {
            //删除pda记录
            dao.deletePda(pdaString);
        }
        if (xfztString != null && xfztString.size() > 0) {
            //回滚拣货表下发状态记录
            for (String id : xfztString) {
                dao.updateXfzt(id, "0");
            }
        }
    }
}
